package com.hhkj.router.demo;

import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import com.hhkj.router.Injector;
import com.hhkj.router.Router;
import com.hhkj.router.TypeToken;
import com.hhkj.router.UriRequest;
import com.hhkj.router.demo.bean.A;
import com.hhkj.router.demo.bean.B;
import com.hhkj.router.demo.bean.PA;
import com.hhkj.router.demo.bean.SA;
import com.hhkj.router.route.ParamType;
import com.hhkj.router.route.RouteInfo;
import com.hhkj.router.service.SerializationService;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 文件名：Injector <br/>
 * 描述：TODO
 *
 * @author wangmingshuo
 * @since 2023/04/26 16:50
 */
public class MyInjector implements Injector {

    private final SerializationService service;

    public MyInjector() {
        service = Router.get().route(SerializationService.class);
        if (service == null) {
            throw new IllegalStateException("serialization service no found.");
        }

        // 参数不为空时，执行  注入query参数
        /*
        val uri = Uri.parse("")
        val names = uri.queryParameterNames
        names.contains("")
         */
    }

    @NonNull
    public Bundle query(@NonNull UriRequest request, @NonNull RouteInfo routeInfo) {
        Uri uri = request.getUri();
        Bundle extras = request.getExtras();
        if (extras == null) {
            extras = new Bundle();
        }
        Map<String, Integer> paramsTypeMap = routeInfo.getParamsType();
        Set<String> keys = uri.getQueryParameterNames();
        if (keys != null && keys.size() > 0) {
            for (String key : keys) {
                String value = uri.getQueryParameter(key);
                Integer paramType = paramsTypeMap == null ? null : paramsTypeMap.get(key);
                addParam(extras, key, value, paramType != null ? paramType : ParamType.STRING);
            }
        }
        return extras;
    }

    private void addParam(Bundle extras, String key, String value, int paramType) {
        try {
            switch (paramType) {
                case ParamType.BOOLEAN:
                    extras.putBoolean(key, Boolean.parseBoolean(value));
                    break;

                case ParamType.BYTE:
                    extras.putByte(key, Byte.parseByte(value));
                    break;

                case ParamType.SHORT:
                    extras.putShort(key, Short.parseShort(value));
                    break;

                case ParamType.INT:
                    extras.putInt(key, Integer.parseInt(value));
                    break;

                case ParamType.LONG:
                    extras.putLong(key, Long.parseLong(value));
                    break;

                case ParamType.CHAR:
                    extras.putChar(key, value.charAt(0));
                    break;

                case ParamType.FLOAT:
                    extras.putFloat(key, Float.parseFloat(value));
                    break;

                case ParamType.DOUBLE:
                    extras.putDouble(key, Double.parseDouble(value));
                    break;

                case ParamType.PARCELABLE:
                    Parcelable parcelable = service.parseObject(value, new TypeToken<PA>() {
                    }.getType());
                    extras.putParcelable(key, parcelable);
                    break;

                case ParamType.SERIALIZABLE:
                    Serializable serializable = service.parseObject(value, new TypeToken<SA>() {
                    }.getType());
                    extras.putSerializable(key, serializable);
                    break;

                default:
                    // OBJECT/STRING 直接使用String
                    extras.putString(key, value);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void inject(Object object) {
        SecondActivity activity = (SecondActivity) object;
        Bundle args = activity.getIntent().getExtras();
        activity.progress = args.getInt("progress", 0);
        activity.toast = args.getString("toast");
        activity.a = service.parseObject(args.getString("a"), new TypeToken<A<B>>() {
        }.getType());
        activity.list = service.parseObject(args.getString("list"), new TypeToken<List<String>>() {
        }.getType());
        // TODO 类型转换
        activity.sa = (SA) args.getSerializable("sa");
        activity.pa = args.getParcelable("pa");
    }

}
